const globalConf = require('./conf');
const fs = require('fs');
const net = require('net');

const server = net.createServer();
server.listen(globalConf.port, '127.0.0.1');
server.on('connection', socket => {
    console.log('客户端已连接');
    socket.on('data', data => {
        try {
            console.log(1);
            const url = data.toString().split('\r\n')[0].split(' ')[1];
            const resource = fs.readFileSync(__dirname + globalConf.path + url);
            socket.write('HTTP 200OK\r\n\r\n');
            socket.write(resource);
        } catch(e) {
            socket.write('HTTP 404Not Found\r\n\r\n<html><body><h1>Page Not Found</h1></body></html>');
        }
        socket.end();
    })
})
server.on('listening', () => {
    console.log('服务器启动了');
})